# -*- coding: utf-8 -*-
# coding:utf-8

from django.db import models
import wejudge.apps.account.models as AccountModel
import wejudge.apps.problem.models as ProblemModel

MOSS_TASKS_MODEL_TYPE = (
    ('asgn', 'Asgn Model Flag'),            # 作业标志
    ('contest', 'Contest Model Flag')       # 比赛标志
)

MOSS_TASKS_STATUS = (
    (0, 'Pending'),             # 排队中
    (1, 'Analyzing'),           # 正在分析
    (2, 'Analysis Failed'),     # 分析失败
    (3, 'Analysis Success')     # 分析完成
)


class MossTasks(models.Model):

    # Moss查重任务队列

    user = models.ForeignKey(AccountModel.User, null=True, blank=True)              # 查重任务发起人

    create_time = models.IntegerField(default=0)                                    # 查重任务发起时间

    name = models.CharField(max_length=100)                                         # 查重任务名称

    celery_task_id = models.CharField(max_length=200, blank=True, null=True, default="")        # CeleryTask

    status = models.IntegerField(default=0, choices=MOSS_TASKS_STATUS)              # 队列状态

    process = models.FloatField(default=0)                                          # 查重完成度

    start_analysis_time = models.IntegerField(default=0)                            # 开始进入Pending Analysis的时间戳

    setting_ratio = models.IntegerField(default=90)                                 # 查重敏感的重复率（默认值是90%）

    setting_line_matched = models.IntegerField(default=0)                           # 查重敏感的重复行数（0表示不限制）

    # Model 类型，决定
    model_type = models.CharField(default='asgn', max_length=20, null=False, blank=False, choices=MOSS_TASKS_MODEL_TYPE)

    # Model 关联ID，关联哪个模型的话由model_type决定，并且这里，不再和模块耦合
    model_id = models.IntegerField(default=0)

    result_total = models.IntegerField(default=0)                                   # 查重结果数据总数（敏感部分）

    result_file = models.CharField(max_length=255)                                  # 结果数据文件

    is_public = models.BooleanField(default=False)                                  # 是否公开

    def __unicode__(self):

        return u"id=%d" % self.id


class MossStatusResult(models.Model):

    # Moss 评测状态结果队列

    status1 = models.ForeignKey(ProblemModel.JudgeStatus, null=True, blank=True, related_name="moss_result_status1")

    status2 = models.ForeignKey(ProblemModel.JudgeStatus, null=True, blank=True, related_name="moss_result_status2")

    ratio1 = models.FloatField(default=0)

    ratio2 = models.FloatField(default=0)

    url = models.TextField(default="")

    def __unicode__(self):

        return u"id=%d" % self.id
